package com.china08.yxyapi.api.manage.tec.teaching.lecture.res;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;

/**
 * 备课包 -- 下载资源-- 浏览量+1
 * 
 * @author yaoguanglin
 *
 */
@CrossOrigin
@RestController
public class LectureUpdResPvController extends YxyApiBaseController {

    @Autowired
    private YxyResResourceRepository yrrr;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/res/pv/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "id") String id) {
        RespModel respModel = new RespModel();

        // 资源下载量 +1
        if (null != id) {
            EsResourcePvUpdateUtil.updateEsResoure(id + "_2");
            YxyResResource resResource = yrrr.findById(id);
            resResource.setDownloadCount(resResource.getDownloadCount() + 1l);
            yrrr.save(resResource);
        }
        respModel.setMessage("成功");
        return respModel;
    }

    protected class RespModel extends YxyApiRespModel {
    }
}
